import {
  Notification,
  addPage,
  delay,
  i18n,
  request,
  $,
  NamedPage,
} from "@hydrooj/ui-default";
addPage(
  new NamedPage("schoolmanage_adduser", () => {
    async function post(draft) {
      try {
        const res = await request.post("", {
          users: $('[name="users"]').val(),
          draft,
        });
        if (!draft) {
          Notification.success(i18n("Created {0} users.", res.users.length));
          await delay(2000);
          window.location.reload();
        } else {
          $('[name="messages"]').text(res.messages.join("\n"));
        }
      } catch (error) {
        Notification.error(error.message);
      }
    }

    $('[name="preview"]').on("click", () => post(true));
    $('[name="submit"]').on("click", () => post(false));
  })
);
